package com.nbgroup.service.impl;

import com.nbgroup.mapper.MenuMapper;
import com.nbgroup.pojo.RoleMenu;
import com.nbgroup.pojo.dto.MenuDto;
import com.nbgroup.pojo.vo.MenuTreeselectVO;
import com.nbgroup.pojo.vo.MenuVo;
import com.nbgroup.pojo.vo.RoleMenuResult;
import com.nbgroup.service.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public List<MenuVo> allList(MenuVo menuvo) {
        return menuMapper.allList(menuvo.getMenuName(),menuvo.getStatus());
    }

    @Override
    public void addMenu(MenuVo menuVo) {
        menuVo.setCreateTime(LocalDateTime.now());
        menuMapper.insert(menuVo);
    }

    @Override
    public MenuVo selectById(Integer menuId) {
        return menuMapper.selectId(menuId);
    }

    @Override
    public void update(MenuDto menuDto) {
        MenuVo menuVo = new MenuVo();
        BeanUtils.copyProperties(menuDto,menuVo);
        menuMapper.updateById(menuVo);
    }

    @Override
    public void delete(Integer menuId) {
        menuMapper.deleteById(menuId);
    }

    @Override
    public List<MenuTreeselectVO> queryAllMenuS() {
    List<MenuTreeselectVO> menuVos = menuMapper.queryAllMenuS();
        return menuVos;
    }

    @Override
    public RoleMenuResult queryMenuByRole(Long roleId) {
        RoleMenuResult roleMenuResult = new RoleMenuResult();
        List<Integer> menus = menuMapper.queryMenuByRole(roleId);
        List<MenuTreeselectVO> menuTreeselectVOS = menuMapper.queryAllMenuS();
        roleMenuResult.setMenus(menuTreeselectVOS);
        roleMenuResult.setCheckedKeys(menus);
        return roleMenuResult;
    }
}
